<?php namespace Home\Controller;

use \Common\Model\User;

//个人中心控制器
class SettingsController extends CommonController{
	protected $model;
	public function __auto(){
		$this->model=new User();
	}

	//个人信息页面
	public function index(){
		$uid=$_SESSION['homeInfo']['uid'];
		//修改个人信息
		if (IS_POST) {
			$settings=Q('post.');
			//判断是否修改头像
			if (!isset($settings['picture'])){
				$this->model
				->where("uid={$uid}")
				->update([
					'sex'=>$settings['sex'],
					'username'=>$settings['username']
				]);
			}else{
				$this->model
				->where("uid={$uid}")
				->update([
					'sex'=>$settings['sex'],
					'username'=>$settings['username'],
					'picture'=>$settings['picture']
				]);
			}
			//修改session
			$_SESSION['homeInfo']['username']=$settings['username'];
			View::success('修改成功');
		}
		//获得并分配个人信息
		$data=$this->model->where("uid={$uid}")->find();
		View::with('data',$data)->make();
	}

	//邮箱绑定
	public function email(){
		View::make();
	}

	//修改密码
	public function editpwd(){
		if (IS_POST) {
			if(!$this->model->editpassword()) View::error($this->model->getError());
            View::success('修改成功');
		}
		$uid=$_SESSION['homeInfo']['uid'];
		$data=$this->model->where("uid={$uid}")->pluck('account');
		View::with('data',$data)->make();
	}

	//uploadify异步
	public function upload(){
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}

	//uploadify异步删除
	public function delUpload(){
		$path=Q('post.oldImg');
		if (unlink($path)) {
			echo 1;
		}else{
			echo 0;
		}
	}

}